package myaction.student;

import myutil.DbUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;

public class StudentOptCancalAction extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String id = request.getParameter("id");
        try {
            Connection conn = DbUtil.getConn();
//          删除我的课程中的此课程记录
            String sql1 = "delete from t_opt where o_id=?;";
            PreparedStatement ps = conn.prepareStatement(sql1);
            ps.setString(1, id);
            ps.executeUpdate();

            System.out.println(id + "号课程已删除！");
//          将此课程在t_courses表中变为可选
            String sql2 = "update t_courses set c_isable='1' where c_id=?;";
            PreparedStatement ps2 = conn.prepareStatement(sql2);
            ps2.setInt(1, Integer.parseInt(id));
            ps2.executeUpdate();
            ps.close();
            ps2.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        response.sendRedirect("StudentOptFindAll");
    }
}
